home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / c / pro14 / getpatfl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-04-02  |  1.4 KB  |  47 lines

  1. #include <stdio.h>
  2. /* #include <sys/types.h>
  3. #include <sys/stat.h> */
  4. #include <stat.h>        /* for C86 */
  5. #include "bm.h"
  6. int
  7. GetPatFile(PatFile, DescVec)
  8. char *PatFile;
  9. struct PattDesc *DescVec[];
  10. /* read patterns from a file and set up a pattern descriptor vector */
  11. {
  12.     extern char *malloc();
  13.     FILE *PFile;
  14. /*    struct stat StatBuff;    */
  15.     struct fbuf StatBuff;    /* for C86 */
  16.     int PatSize; /* the number of chars in all the patterns */
  17.     char *PatBuff; /* hold the patterns */
  18.     
  19.     /* find out how big the patterns are */
  20.     /* use stat instead of fstat for C86 */
  21.     if (stat(PatFile,&StatBuff) == -1) {
  22.         fprintf(stderr,"bm: can't stat %s\n",PatFile);
  23.         exit(2);
  24.     }
  25.     PatSize = StatBuff.st_size;
  26.     if (!PatSize) {
  27.         fprintf(stderr,"bm: pattern file is empty\n");
  28.         exit(2);
  29.     } /* if */
  30.     if (!(PFile = fopen(PatFile,"r"))) {
  31.         fprintf(stderr,"bm: can't open pattern file %s\n",PatFile);
  32.         exit(2);
  33.     } /* if */
  34.     if (!(PatBuff = malloc(PatSize))) {
  35.         fprintf(stderr,"bm: insufficient memory to store patterns\n");
  36.         exit(2);
  37.     } /* if */
  38.     fread(PatBuff,1,PatSize,PFile); /* get the patterns */
  39.     /* make sure the patterns are null-terminated. We can't have
  40.     * nulls in the patterns */
  41.     if (PatBuff[PatSize-1] == '\n')
  42.         PatBuff[PatSize-1] = '\0';
  43.     else
  44.         PatBuff[PatSize] = '\0';
  45.     return(MkDescVec(DescVec,PatBuff));
  46. } /* GetPatFile */
  47.